package com.xinghai.web.controller.api;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xinghai.common.core.controller.BaseController;
import com.xinghai.common.core.domain.AjaxResult;
import com.xinghai.common.utils.DataUtil;
import com.xinghai.common.utils.DateUtils;
import com.xinghai.common.utils.PageUtils;
import com.xinghai.common.utils.StringUtils;
import com.xinghai.common.utils.poi.ExcelUtil;
import com.xinghai.system.domain.*;
import com.xinghai.system.domain.entity.StatisticsDataVO;
import com.xinghai.system.domain.vo.TodayDataVO;
import com.xinghai.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

/**
 * 统计api接口
 *
 * @author xinghai
 * @date 2024-12-04
 */
@RestController
@RequestMapping("/api/statistics")
public class QdStatisticsApiController extends BaseController
{
    @Autowired
    private IQdUserService userService;
    @Autowired
    private IQdStoreService storeService;
    @Autowired
    private IQdUserWalletService userWalletService;
    @Autowired
    private IQdUserWalletLogService walletLogService;


    @Autowired
    private IQdOrderService orderService;
    @Autowired
    private IQdPayService payService;
    @Autowired
    private IQdPayOrderInfoService payOrderInfoService;

    @Autowired
    private IQdPayOrderService payOrderService;

    @Autowired
    private IQdDealInfoService dealInfoService;

    /**
     * 我的数据
     * @return
     */
    @GetMapping("/getMyData")
    public AjaxResult getMyData(String date)
    {
        QdPay pay = new QdPay();
        //插入时间查询
        pay.setBuyerUserId(StpUtil.getLoginId(0L));
        List<QdPay> buyList = payService.list(pay);
        //累计
        BigDecimal allBuyMoney = buyList.stream().map(QdPay::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        int allBuyNum = buyList.size();
        String day1 = DateUtils.getWeekStartData(date);
        String day2 = DateUtils.getLastWeekStartData(date);
        String day3 = DateUtils.getTomorrowDate(DateUtils.parseDate(date));
        Date date0 = DateUtils.parseDate(date);
        Date date1 = DateUtils.parseDate(day1);
        Date date2 = DateUtils.parseDate(day2);
        Date date3 = DateUtils.parseDate(day3);

        //本周买入
        List<QdPay> weekBuyList = buyList.stream()
                .filter(m -> date1.compareTo(m.getCreateTime()) < 1 && date2.compareTo(m.getCreateTime()) > 0).collect(Collectors.toList());
        BigDecimal weekBuyMoney = weekBuyList.stream().map(QdPay::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        int weekBuyNum = weekBuyList.size();
        //今日
        List<QdPay> todayBuyList = buyList.stream()
                .filter(m -> date0.compareTo(m.getCreateTime()) < 1 && date3.compareTo(m.getCreateTime()) > 0).collect(Collectors.toList());
        BigDecimal todayBuyMoney = todayBuyList.stream().map(QdPay::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        int todayBuyNum = todayBuyList.size();

        List<StatisticsDataVO> list = new ArrayList<>();
        StatisticsDataVO a1 = new StatisticsDataVO();
        a1.setName("买入单数");
        a1.setTodayData(String.valueOf(todayBuyNum));
        a1.setWeekData(String.valueOf(weekBuyNum));
        a1.setAllData(String.valueOf(allBuyNum));
        list.add(a1);

        StatisticsDataVO a2 = new StatisticsDataVO();
        a2.setName("买入金额");
        a2.setTodayData(String.valueOf(todayBuyMoney));
        a2.setWeekData(String.valueOf(weekBuyMoney));
        a2.setAllData(String.valueOf(allBuyMoney));
        list.add(a2);

        QdPay pay1 = new QdPay();
        //插入时间查询
        pay1.setSellerUserId(StpUtil.getLoginId(0L));
        List<QdPay> sellList = payService.list(pay1);
        //累计
        BigDecimal allSellMoney = sellList.stream().map(QdPay::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        int allSellNum = sellList.size();
        //本周买入
        List<QdPay> weekSellList = sellList.stream()
                .filter(m -> date1.compareTo(m.getCreateTime()) < 1 && date2.compareTo(m.getCreateTime()) > 0).collect(Collectors.toList());
        BigDecimal weekSellMoney = weekSellList.stream().map(QdPay::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        int weekSellNum = weekSellList.size();
        //今日
        List<QdPay> todaySellList = sellList.stream()
                .filter(m -> date0.compareTo(m.getCreateTime()) < 1 && date3.compareTo(m.getCreateTime()) > 0).collect(Collectors.toList());
        BigDecimal todaySellMoney = todaySellList.stream().map(QdPay::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        int todaySellNum = todaySellList.size();

        StatisticsDataVO a3 = new StatisticsDataVO();
        a3.setName("卖出单数");
        a3.setTodayData(String.valueOf(todaySellNum));
        a3.setWeekData(String.valueOf(weekSellNum));
        a3.setAllData(String.valueOf(allSellNum));
        list.add(a3);

        StatisticsDataVO a4 = new StatisticsDataVO();
        a4.setName("卖出金额");
        a4.setTodayData(String.valueOf(todaySellMoney));
        a4.setWeekData(String.valueOf(weekSellMoney));
        a4.setAllData(String.valueOf(allSellMoney));
        list.add(a4);

        QdOrder order = new QdOrder();
        order.setSellerUserId(StpUtil.getLoginId(0L));
        order.setSellerStatus("2");
        List<QdOrder> orderList = orderService.list(order);
        //累计
        BigDecimal allGoodMoney = orderList.stream().map(m->m.getPrice().subtract(m.getOldPrice())).reduce(BigDecimal.ZERO, BigDecimal::add);
        int allGoodNum = orderList.size();
        //本周买入
        List<QdOrder> weekGoodList = orderList.stream()
                .filter(m -> date1.compareTo(m.getDealTime()) < 1 && date2.compareTo(m.getDealTime()) > 0).collect(Collectors.toList());
        BigDecimal weekGoodMoney = weekGoodList.stream().map(m->m.getPrice().subtract(m.getOldPrice())).reduce(BigDecimal.ZERO, BigDecimal::add);
        int weekGoodNum = weekGoodList.size();
        //今日
        List<QdOrder> todayGoodList = orderList.stream()
                .filter(m -> date0.compareTo(m.getDealTime()) < 1 && date3.compareTo(m.getDealTime()) > 0).collect(Collectors.toList());
        BigDecimal todayGoodMoney = todayGoodList.stream().map(m->m.getPrice().subtract(m.getOldPrice())).reduce(BigDecimal.ZERO, BigDecimal::add);
        int todayGoodNum = todayGoodList.size();

        StatisticsDataVO a5 = new StatisticsDataVO();
        a5.setName("商品收益");
        a5.setTodayData(String.valueOf(todayGoodMoney));
        a5.setWeekData(String.valueOf(weekGoodMoney));
        a5.setAllData(String.valueOf(allGoodMoney));
        list.add(a5);
        return success(list);
    }

    /**
     * 直推数据
     * @return
     */
    @GetMapping("/getPushData")
    public AjaxResult getPushData(String date)
    {
        QdPay pay = new QdPay();
        QdUser user = new QdUser();
        user.setSuperId(StpUtil.getLoginId(0L));
        List<QdUser> list1 = userService.list(user);
        if (list1.isEmpty()){
            List<StatisticsDataVO> list = new ArrayList<>();
            StatisticsDataVO a1 = new StatisticsDataVO();
            a1.setName("买入单数");
            a1.setTodayData("0");
            a1.setWeekData("0");
            a1.setAllData("0");
            list.add(a1);

            StatisticsDataVO a2 = new StatisticsDataVO();
            a2.setName("买入金额");
            a2.setTodayData("0");
            a2.setWeekData("0");
            a2.setAllData("0");
            list.add(a2);

            StatisticsDataVO a3 = new StatisticsDataVO();
            a3.setName("卖出单数");
            a3.setTodayData("0");
            a3.setWeekData("0");
            a3.setAllData("0");
            list.add(a3);

            StatisticsDataVO a4 = new StatisticsDataVO();
            a4.setName("卖出金额");
            a4.setTodayData("0");
            a4.setWeekData("0");
            a4.setAllData("0");
            list.add(a4);

            StatisticsDataVO a5 = new StatisticsDataVO();
            a5.setName("直推收益");
            a5.setTodayData("0");
            a5.setWeekData("0");
            a5.setAllData("0");
            list.add(a5);
            return success(list);
        }
        String string = list1.stream().map(m -> String.valueOf(m.getId())).collect(Collectors.joining(","));

        //插入时间查询
        pay.setBuyerUserIds(string);
        List<QdPay> buyList = payService.list(pay);
        //累计
        BigDecimal allBuyMoney = buyList.stream().map(QdPay::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        int allBuyNum = buyList.size();
        String day1 = DateUtils.getWeekStartData(date);
        String day2 = DateUtils.getLastWeekStartData(date);
        String day3 = DateUtils.getLastWeekStartData(date);
        Date date0 = DateUtils.parseDate(date);
        Date date1 = DateUtils.parseDate(day1);
        Date date2 = DateUtils.parseDate(day2);
        Date date3 = DateUtils.parseDate(day3);
        //本周买入
        List<QdPay> weekBuyList = buyList.stream()
                .filter(m -> date1.compareTo(m.getCreateTime()) < 1 && date2.compareTo(m.getCreateTime()) > 0).collect(Collectors.toList());
        BigDecimal weekBuyMoney = weekBuyList.stream().map(QdPay::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        int weekBuyNum = weekBuyList.size();
        //今日
        List<QdPay> todayBuyList = buyList.stream()
                .filter(m -> date0.compareTo(m.getCreateTime()) < 1 && date3.compareTo(m.getCreateTime()) > 0).collect(Collectors.toList());
        BigDecimal todayBuyMoney = todayBuyList.stream().map(QdPay::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        int todayBuyNum = todayBuyList.size();

        List<StatisticsDataVO> list = new ArrayList<>();
        StatisticsDataVO a1 = new StatisticsDataVO();
        a1.setName("买入单数");
        a1.setTodayData(String.valueOf(todayBuyNum));
        a1.setWeekData(String.valueOf(weekBuyNum));
        a1.setAllData(String.valueOf(allBuyNum));
        list.add(a1);

        StatisticsDataVO a2 = new StatisticsDataVO();
        a2.setName("买入金额");
        a2.setTodayData(String.valueOf(todayBuyMoney));
        a2.setWeekData(String.valueOf(weekBuyMoney));
        a2.setAllData(String.valueOf(allBuyMoney));
        list.add(a2);

        QdPay pay1 = new QdPay();
        //插入时间查询
        pay1.setSellerUserIds(string);
        List<QdPay> sellList = payService.list(pay1);
        //累计
        BigDecimal allSellMoney = sellList.stream().map(QdPay::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        int allSellNum = sellList.size();
        //本周买入
        List<QdPay> weekSellList = sellList.stream()
                .filter(m -> date1.compareTo(m.getCreateTime()) < 1 && date2.compareTo(m.getCreateTime()) > 0).collect(Collectors.toList());
        BigDecimal weekSellMoney = weekSellList.stream().map(QdPay::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        int weekSellNum = weekSellList.size();
        //今日
        List<QdPay> todaySellList = sellList.stream()
                .filter(m -> date0.compareTo(m.getCreateTime()) < 1 && date3.compareTo(m.getCreateTime()) > 0).collect(Collectors.toList());
        BigDecimal todaySellMoney = todaySellList.stream().map(QdPay::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        int todaySellNum = todaySellList.size();

        StatisticsDataVO a3 = new StatisticsDataVO();
        a3.setName("卖出单数");
        a3.setTodayData(String.valueOf(todaySellNum));
        a3.setWeekData(String.valueOf(weekSellNum));
        a3.setAllData(String.valueOf(allSellNum));
        list.add(a3);

        StatisticsDataVO a4 = new StatisticsDataVO();
        a4.setName("卖出金额");
        a4.setTodayData(String.valueOf(todaySellMoney));
        a4.setWeekData(String.valueOf(weekSellMoney));
        a4.setAllData(String.valueOf(allSellMoney));
        list.add(a4);

        QdUserWalletLog userWalletLog = new QdUserWalletLog();
        userWalletLog.setUserId(StpUtil.getLoginId(0L));
        userWalletLog.setType("1");
        List<QdUserWalletLog> walletLogList = walletLogService.list(userWalletLog);
        BigDecimal allPushMoney = walletLogList.stream().map(QdUserWalletLog::getChangeAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        List<QdUserWalletLog> weekPushList = walletLogList.stream()
                .filter(m -> date1.compareTo(m.getCreateTime()) < 1 && date2.compareTo(m.getCreateTime()) > 0).collect(Collectors.toList());
        BigDecimal weekPushMoney = weekPushList.stream().map(QdUserWalletLog::getChangeAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        List<QdUserWalletLog> todayPushList = walletLogList.stream()
                .filter(m -> date0.compareTo(m.getCreateTime()) < 1 && date3.compareTo(m.getCreateTime()) > 0).collect(Collectors.toList());
        BigDecimal todayPushMoney = todayPushList.stream().map(QdUserWalletLog::getChangeAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        StatisticsDataVO a5 = new StatisticsDataVO();
        a5.setName("直推佣金");
        a5.setTodayData(String.valueOf(todayPushMoney));
        a5.setWeekData(String.valueOf(weekPushMoney));
        a5.setAllData(String.valueOf(allPushMoney));
        list.add(a5);
        return success(list);
    }
    /**
     * 直推数据明细
     * @return
     */
    @GetMapping("/getPushDataInfo")
    public AjaxResult getPushDataInfo(String date,Integer pageNum,Integer pageSize)
    {
        PageUtils.startPage(pageNum,pageSize);
        PageUtils.orderBy("todaySellData desc");
        List<Map<String,Object>> list = payService.pushListInfo(date,StpUtil.getLoginId(""));
        return success(list);
    }

    /**
     * 团队数据
     * @return
     */
    @GetMapping("/getTeamData")
    public AjaxResult getTeamData(String date)
    {
        QdPay pay = new QdPay();
        //插入时间查询
        pay.setUserIdsBuyer(StpUtil.getLoginId(""));
        List<QdPay> buyList = payService.list(pay);
        //累计
        BigDecimal allBuyMoney = buyList.stream().map(QdPay::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        int allBuyNum = buyList.size();
        String day1 = DateUtils.getWeekStartData(date);
        String day2 = DateUtils.getLastWeekStartData(date);
        String day3 = DateUtils.getLastWeekStartData(date);
        Date date0 = DateUtils.parseDate(date);
        Date date1 = DateUtils.parseDate(day1);
        Date date2 = DateUtils.parseDate(day2);
        Date date3 = DateUtils.parseDate(day3);
        //本周买入
        List<QdPay> weekBuyList = buyList.stream()
                .filter(m -> date1.compareTo(m.getCreateTime()) < 1 && date2.compareTo(m.getCreateTime()) > 0).collect(Collectors.toList());
        BigDecimal weekBuyMoney = weekBuyList.stream().map(QdPay::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        int weekBuyNum = weekBuyList.size();
        //今日
        List<QdPay> todayBuyList = buyList.stream()
                .filter(m -> date0.compareTo(m.getCreateTime()) < 1 && date3.compareTo(m.getCreateTime()) > 0).collect(Collectors.toList());
        BigDecimal todayBuyMoney = todayBuyList.stream().map(QdPay::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        int todayBuyNum = todayBuyList.size();

        List<StatisticsDataVO> list = new ArrayList<>();
        StatisticsDataVO a1 = new StatisticsDataVO();
        a1.setName("买入单数");
        a1.setTodayData(String.valueOf(todayBuyNum));
        a1.setWeekData(String.valueOf(weekBuyNum));
        a1.setAllData(String.valueOf(allBuyNum));
        list.add(a1);

        StatisticsDataVO a2 = new StatisticsDataVO();
        a2.setName("买入金额");
        a2.setTodayData(String.valueOf(todayBuyMoney));
        a2.setWeekData(String.valueOf(weekBuyMoney));
        a2.setAllData(String.valueOf(allBuyMoney));
        list.add(a2);

        QdPay pay1 = new QdPay();
        //插入时间查询
        pay1.setUserIdsSeller(StpUtil.getLoginId(""));
        List<QdPay> sellList = payService.list(pay1);
        //累计
        BigDecimal allSellMoney = sellList.stream().map(QdPay::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        int allSellNum = sellList.size();
        //本周买入
        List<QdPay> weekSellList = sellList.stream()
                .filter(m -> date1.compareTo(m.getCreateTime()) < 1 && date2.compareTo(m.getCreateTime()) > 0).collect(Collectors.toList());
        BigDecimal weekSellMoney = weekSellList.stream().map(QdPay::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        int weekSellNum = weekSellList.size();
        //今日
        List<QdPay> todaySellList = sellList.stream()
                .filter(m -> date0.compareTo(m.getCreateTime()) < 1 && date3.compareTo(m.getCreateTime()) > 0).collect(Collectors.toList());
        BigDecimal todaySellMoney = todaySellList.stream().map(QdPay::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        int todaySellNum = todaySellList.size();

        StatisticsDataVO a3 = new StatisticsDataVO();
        a3.setName("卖出单数");
        a3.setTodayData(String.valueOf(todaySellNum));
        a3.setWeekData(String.valueOf(weekSellNum));
        a3.setAllData(String.valueOf(allSellNum));
        list.add(a3);

        StatisticsDataVO a4 = new StatisticsDataVO();
        a4.setName("卖出金额");
        a4.setTodayData(String.valueOf(todaySellMoney));
        a4.setWeekData(String.valueOf(weekSellMoney));
        a4.setAllData(String.valueOf(allSellMoney));
        list.add(a4);
        return success(list);
    }

    @GetMapping("/getTodayYDData")
    public AjaxResult getTodayYDData(String search,String type)
    {
        QdUser qdUser = userService.getByLoginId(StpUtil.getLoginId(""));
        QdOrder order = new QdOrder();
        if ("1".equals(type)){
            order.setSearchValue(search);
        }
        order.setBuyerFlag(true);
        order.setStatus("0");
        order.setStoreId(qdUser.getAscriptionStore());
        order.setDealTime(new Date());
        order.setBuyerType("1,2,3,4");
//        if (type != null){
//            if ("1".equals(type)){
//                order.setBuyerType("1,2,3,4");
//            }
//            if ("2".equals(type)){
//                order.setBuyerType("2,4");
//            }
//        }
        List<QdOrder> list = orderService.list(order);
        if ("2".equals(type)){
            String collect = list.stream().map(m -> String.valueOf(m.getBuyerUserId())).collect(Collectors.joining(","));
            QueryWrapper<QdUser> queryWrapper = new QueryWrapper<>();
            queryWrapper.apply("not FIND_IN_SET(id,'"+collect+"')");
            queryWrapper.eq("ascription_store",qdUser.getAscriptionStore());
            if (StringUtils.isNotBlank(search)){
                queryWrapper.and(m->m.like("user_phone",search).or().like("nick_name",search));
            }
            List<QdUser> userList = userService.list(queryWrapper);
            List<TodayDataVO> todayDataVOList = userList.stream().map(m -> {
                TodayDataVO todayDataVO = new TodayDataVO();
                todayDataVO.setNickName(m.getNickName());
                todayDataVO.setUserPhone(m.getUserPhone());
                todayDataVO.setUserId(m.getId());
                todayDataVO.setAmount(BigDecimal.ZERO);
                todayDataVO.setOrderNum(0);
                return todayDataVO;
            }).collect(Collectors.toList());
            return success(todayDataVOList);
        }
        List<TodayDataVO> collect = list.stream().collect(Collectors.toMap(QdOrder::getBuyerUserPhone, a -> a, (o1, o2) -> {
            if (o1.getOrderAmount() == null) {
                o1.setOrderAmount(o1.getPrice());
            }
            if (o2.getOrderAmount() == null) {
                o2.setOrderAmount(o2.getPrice());
            }
            o1.setOrderAmount(o1.getOrderAmount().add(o2.getOrderAmount()));
            if (o1.getOrderNum() == null) {
                o1.setOrderNum(1);
            }
            if (o2.getOrderNum() == null) {
                o2.setOrderNum(1);
            }
            o1.setOrderNum(o1.getOrderNum() + o2.getOrderNum());
            return o1;
        })).values().stream().map(m -> {
            TodayDataVO todayDataVO = new TodayDataVO();
            todayDataVO.setNickName(m.getBuyerUserName());
            todayDataVO.setUserPhone(m.getBuyerUserPhone());
            todayDataVO.setUserId(m.getBuyerUserId());
            todayDataVO.setAmount(m.getOrderAmount()==null?m.getPrice():m.getOrderAmount());
            todayDataVO.setOrderNum(m.getOrderNum()==null?1:m.getOrderNum());
            return todayDataVO;
        }).sorted(Comparator.comparing(TodayDataVO::getAmount).reversed()).collect(Collectors.toList());

        return success(collect);
    }

    @GetMapping("/getMemberMoneyData")
    public AjaxResult getMemberMoneyData(String search)
    {
        QdUser qdUser = userService.getByLoginId(StpUtil.getLoginId(""));
        QdUser query = new QdUser();
        query.setSearchValue(search);
        query.setAscriptionStore(qdUser.getAscriptionStore());
        List<QdUser> list = userService.list(query);
        String string = list.stream().map(m -> String.valueOf(m.getId())).collect(Collectors.joining(","));
        QdUserWallet wallet = new QdUserWallet();
        wallet.setUserIds(string);
        List<QdUserWallet> list1 = userWalletService.list(wallet);
        QdUserWalletLog walletLog = new QdUserWalletLog();
        walletLog.setUserIds(string);
        walletLog.setCreateTime(new Date());
        List<QdUserWalletLog> logList = walletLogService.list(walletLog);
        List<Map<String, Object>> mapList = list1.stream().map(m -> {
            Map<String, Object> map = new HashMap<>();
            map.put("nickName", m.getUserName());
            map.put("userPhone", m.getUserPhone());
            BigDecimal reduce = logList.stream().filter(l -> l.getUserId().equals(m.getUserId())).map(QdUserWalletLog::getChangeAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
            map.put("todayAmount", reduce);//今日佣金
            map.put("allAmount", m.getAllWallet());//累计佣金
            map.put("withdrawalAmount", m.getWithdrawalWallet());//可提现佣金
            map.put("amount", m.getWithdrawalEndWallet());//累计提现
            return map;
        }).collect(Collectors.toList());
        return success(mapList);
    }
    @GetMapping("/getMemberIncomeData")
    public AjaxResult getMemberIncomeData(String search)
    {
        QdUser qdUser = userService.getByLoginId(StpUtil.getLoginId(""));
        QdUser query = new QdUser();
        query.setSearchValue(search);
        query.setAscriptionStore(qdUser.getAscriptionStore());
        List<QdUser> list = userService.list(query);
        String string = list.stream().map(m -> String.valueOf(m.getId())).collect(Collectors.joining(","));
        QdUserWallet wallet = new QdUserWallet();
        wallet.setUserIds(string);
        List<QdUserWallet> list1 = userWalletService.list(wallet);
        List<Map<String, Object>> mapList = list1.stream().map(m -> {
            Map<String, Object> map = new HashMap<>();
            map.put("nickName", m.getUserName());
            map.put("userPhone", m.getUserPhone());
            map.put("allAmount", m.getAllWallet());
            map.put("allListingFee",m.getListingFee());
            map.put("allGoodAmount",m.getAchievement().subtract(m.getPurchaseAmount()));
            map.put("allIncome",m.getAllWallet().add(m.getAchievement().subtract(m.getPurchaseAmount())).subtract(m.getListingFee()));
            return map;
        }).collect(Collectors.toList());
        return success(mapList);
    }
    @GetMapping("/getMemberOrderData")
    public AjaxResult getMemberOrderData(String search)
    {
        QdUser qdUser = userService.getByLoginId(StpUtil.getLoginId(""));
        QdUser query = new QdUser();
        query.setSearchValue(search);
        query.setAscriptionStore(qdUser.getAscriptionStore());
        List<QdUser> list = userService.list(query);
        String string = list.stream().map(m -> String.valueOf(m.getId())).collect(Collectors.joining(","));
        QdUserWallet wallet = new QdUserWallet();
        wallet.setUserIds(string);
        List<QdUserWallet> list1 = userWalletService.list(wallet);
        List<Map<String, Object>> mapList = list1.stream().map(m -> {
            Map<String, Object> map = new HashMap<>();
            map.put("nickName", m.getUserName());
            map.put("userPhone", m.getUserPhone());
            map.put("allGoodAmount",m.getAchievement().subtract(m.getPurchaseAmount()));
            BigDecimal subtract = m.getAllWallet().add(m.getAchievement().subtract(m.getPurchaseAmount())).subtract(m.getListingFee());
            map.put("allIncome",subtract);
            map.put("diff",subtract.subtract(m.getNowMoney()));
            return map;
        }).collect(Collectors.toList());
        return success(mapList);
    }
    @GetMapping("/getPayOrderList")
    public AjaxResult getPayOrderList()
    {
        String loginId = StpUtil.getLoginId("");
        //获取用户信息
        QdUser user = userService.getByLoginId(loginId);
        PageUtils.orderBy("gen_date desc");
        QdPayOrder payOrder = new QdPayOrder();
        payOrder.setStoreId(user.getAscriptionStore());
        List<QdPayOrder> list = payOrderService.list(payOrder);
        return success(list);
    }
    @GetMapping("/getPayOrderInfo")
    public AjaxResult getPayOrderInfo(String dataId)
    {
        String loginId = StpUtil.getLoginId("");
        //获取用户信息
        QdUser user = userService.getByLoginId(loginId);
        QdPayOrderInfo payOrderInfo = new QdPayOrderInfo();
        payOrderInfo.setLinkPayOrder(Long.valueOf(dataId));
        payOrderInfo.setStoreId(user.getAscriptionStore());
        List<QdPayOrderInfo> list = payOrderInfoService.list(payOrderInfo);
        return success(list);
    }

    /**
     * 导出订单变更列表
     */
    @GetMapping("/downPayOrder")
    public void downPayOrder(HttpServletResponse response, String dataId)
    {
        String loginId = StpUtil.getLoginId("");
        //获取用户信息
        QdUser user = userService.getByLoginId(loginId);
        QdPayOrderInfo payOrderInfo = new QdPayOrderInfo();
        payOrderInfo.setLinkPayOrder(Long.valueOf(dataId));
        payOrderInfo.setStoreId(user.getAscriptionStore());
        List<QdPayOrderInfo> list = payOrderInfoService.list(payOrderInfo);
        list.forEach(m -> {
            QdDealInfo dealInfo = new QdDealInfo();
            dealInfo.setOrderInfoId(m.getId());
            List<QdDealInfo> list1 = dealInfoService.list(dealInfo);
            StringBuffer sb = new StringBuffer();
            if (m.getDiff().compareTo(BigDecimal.ZERO)>0){
                sb.append("应收款来自以下会员：");
            }else if (m.getDiff().compareTo(BigDecimal.ZERO)<0){
                sb.append("应向以下会员支付差价：");
            }else {
                sb.append("不需要支付");
            }
            list1.forEach(l -> {
                sb.append(String.format("%s(%s),%s:%s-%s,支付金额%s元",l.getPayUserName(),l.getPayUserPhone(),l.getPayBankInfo(),l.getPayBankName(),l.getPayBankNo(),l.getAmount()));
            });
            m.setDealInfoStr(sb.toString());
        });
        ExcelUtil<QdPayOrderInfo> util = new ExcelUtil<QdPayOrderInfo>(QdPayOrderInfo.class);
        util.exportExcel(response, list, "平账单数据");
    }


    /**
     * 导出订单变更列表
     */
    @GetMapping("/downPayInfo")
    public void downPayInfo(HttpServletResponse response, String date)
    {
        String loginId = StpUtil.getLoginId("");
        //获取用户信息
        QdUser user = userService.getByLoginId(loginId);
        QdOrder order = new QdOrder();
        order.setStatus("0");
        order.setStoreId(user.getAscriptionStore());
        order.setDealTime(DateUtils.parseDate(date));
        List<QdOrder> list = orderService.list(order);
        ExcelUtil<QdOrder> util = new ExcelUtil<QdOrder>(QdOrder.class);
        util.exportExcel(response, list, "交易数据数据");
    }
}
